package b3.widget.home.town
{
	import as3isolib.geom.Pt;
	
	import b3.model.*;
	import b3.net.*;
	import b3.scene.home.*;
	import b3.scene.home.grid.*;
	import b3.util.*;
	import b3.widget.basic.*;
	
	import com.greensock.*;
	
	import flash.display.*;
	import flash.events.*;
	import flash.filters.*;
	import flash.text.*;
	
	public class AttackIcon extends BasicWidget
	{
		private var iconImg:BasicIcon;
		private var hideFlag:Boolean;
		
		public function AttackIcon()
		{
			super();
			var OperationAttack:Class = ResourceHelper.getInstance().getClassByName("OperationAttack");
			iconImg = new BasicIcon(OperationAttack, "占领", 1, true);
			iconImg.disableMouseOverTip();
			addChild(iconImg);
			this.anchor = ANCHOR_RIGHT_BOTTOM;
			this.x = -180;
			this.y = -156;
			hideFlag = true;
			iconImg.x = width + 180;
			this.addEventListener(MouseEvent.CLICK, onMouseClick);
			this.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
			this.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
		}
		
		private function onMouseOver(event:MouseEvent):void
		{
			var container:Sprite = new Sprite();		
			//提示图标
			var OperationAttack:Class = ResourceHelper.getInstance().getClassByName("OperationAttack");
			var tipIcon:Sprite = new OperationAttack();
			tipIcon.height = 60;
			tipIcon.scaleX = tipIcon.scaleY;
			tipIcon.x = 10;
			tipIcon.y = 0;
			container.addChild(tipIcon);
			//提示信息
			var tipText:TextField = new TextField();
			tipText.selectable = false;
			tipText.autoSize = TextFieldAutoSize.LEFT;
			tipText.defaultTextFormat = new TextFormat("微软雅黑", 25, 0xFFFFFF, true);
			tipText.filters = [new GlowFilter(0x00AAFF, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			tipText.text = "攻击最高楼，占领该城市";
			tipText.x = 65;
			tipText.y = 0;
			container.addChild(tipText);
			//说明信息
			var directionText:TextField = new TextField();
			directionText.selectable = false;
			directionText.autoSize = TextFieldAutoSize.LEFT;
			directionText.defaultTextFormat = new TextFormat("微软雅黑", 12, 0x000000, true);
			directionText.text = "（点击定位最高楼）";
			directionText.x = tipText.x + Math.max((tipText.width - directionText.width) / 2, 0);
			directionText.y = 40;
			container.addChild(directionText);
			HomeScene.getInstance().centerInfoPane.showInfo(container);
		}
		
		private function onMouseOut(event:MouseEvent):void
		{
			HomeScene.getInstance().centerInfoPane.clear();
		}

		public function hide():void
		{
			hideFlag = true;
			TweenLite.to(iconImg, 3, {x: iconImg.width + 180});
		}
		
		public function show():void
		{
			hideFlag = false;
			TweenLite.to(iconImg, 3, {x: 0});
		}
		
		public static function onMouseClick(event:Event):void
		{
			var town:Town = HomeScene.getInstance().getCurrentTown();
			var buildings:Object = town.owner.townModel.buildings;
			var maxHeightBuildingModel:BuildingModel;
			for (var i:String in buildings)
			{
				var buildingModel:BuildingModel = buildings[i];
				if (maxHeightBuildingModel == null || buildingModel.population > maxHeightBuildingModel.population
					|| buildingModel.population == maxHeightBuildingModel.population && buildingModel.id < maxHeightBuildingModel.id)
				{
					maxHeightBuildingModel = buildingModel;
				}
			}
			if (maxHeightBuildingModel != null)
			{
				var buildingGrid:BuildingGrid = town.getBuildingById(maxHeightBuildingModel.id);
				buildingGrid.showArrow();
				buildingGrid.startShine();
				town.setViewCenter(new Pt(buildingGrid.x, buildingGrid.y), true);
			}
		}
		
	}
}